home *** CD-ROM | disk | FTP | other *** search
/ Revista CD Expert 8 / Revista CD Expert nº 08 CD1.iso / Utilitarios / Programacao / Pacific C for DOS / EXAMPLES / QSORTEX.C < prev    next >
C/C++ Source or Header  |  1995-03-08  |  2KB  |  100 lines

  1. #include    <stdio.h>
  2. #include    <stdlib.h>
  3. #include    <time.h>
  4.  
  5. /*
  6.  *    Pacific C QSORT Example.
  7.  *
  8.  *    This program demonstrates the use of the qsort() function
  9.  *    to sort an array.  It also includes an example of random
  10.  *    number generation using the rand() function.  Note the use
  11.  *    of the system time (obtained using time()) to seed the
  12.  *    random number generator via the srand() function.
  13.  */
  14.  
  15. #define    ARRAY_SIZE    64
  16.  
  17. int    array[ARRAY_SIZE];
  18.  
  19. /*
  20.  *    void    make_array(void)
  21.  *
  22.  *    Generate ARRAY_SIZE random elements of "array[]" using the
  23.  *    rand() function.  Note use of time() and srand() to seed
  24.  *    the random number generator.
  25.  */
  26.  
  27. static void
  28. make_array(void)
  29. {
  30.     int    index;
  31.     time_t    seed;
  32.  
  33.     time(&seed);    /* use system time as random seed */
  34.     srand(seed);
  35.     for (index = 0; index != ARRAY_SIZE; index++)
  36.         array[index] = rand();
  37. }
  38.  
  39. /*
  40.  *    void    print_array(void)
  41.  *
  42.  *    Print "array[]" in rows of eight columns.
  43.  */
  44.  
  45. static void
  46. print_array(void)
  47. {
  48.     int    index, col;
  49.  
  50.     col = 0;
  51.     for (index = 0; index != ARRAY_SIZE; index++) {
  52.         printf("%8d", array[index]);
  53.         if (++col == 8) {
  54.             putchar('\n');
  55.             col = 0;
  56.         }
  57.     }
  58.     if (col != 0)
  59.         putchar('\n');
  60. }
  61.  
  62. /*
  63.  *    int    compare_ints(const void * i1, const void * i2)
  64.  *
  65.  *    "Compare function" used by qsort() to compare array elements.
  66.  *    qsort() calls this function with the "const void *" arguments
  67.  *    set to point to a pair of array elements.  To dereference the
  68.  *    "const void *" arguments, a cast to the appropriate type (int *)
  69.  *    must be performed.
  70.  *
  71.  *    compare_ints returns the following values:
  72.  *
  73.  *    0            If *i1 == *i2
  74.  *    negative integer    If *i1 < *i2
  75.  *    positive integer    If *i1 > *i2
  76.  */
  77.  
  78. static int
  79. compare_ints(const void * i1, const void * i2)
  80. {
  81.     return (*(int *) i1) - (*(int *) i2);
  82. }
  83.  
  84. /*
  85.  *    main program.
  86.  */
  87.  
  88. main()
  89. {
  90.     printf("Pacific C QSORT example\n");
  91.     printf("Generating randomised array\n");
  92.     make_array();
  93.     printf("Array before QSORT:\n");
  94.     print_array();
  95.     printf("Sorting ...\n");
  96.     qsort(array, ARRAY_SIZE, sizeof(array[0]), compare_ints);
  97.     printf("Array after QSORT:\n");
  98.     print_array();
  99. }
  100.